/*******************************************************************************
  * @file                   E34.c
  * @Author:                MQjehovah                 mail:MQjehovah@hotmail.com
  * @version                1.0.0
  * @date                   2017-12-22 10:18:04
  * @brief                  
  ******************************************************************************
  * @attention
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "e34.h"
/* Definition ----------------------------------------------------------------*/
ring_buffer E34_RX_BUF;
ring_buffer *pE34_RX_BUF = &E34_RX_BUF;
/* Functions -----------------------------------------------------------------*/
/*******************************************************************************
  * @brief  E34模块初始化             
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void E34_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);  //使能GPIO时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13; //GPIOB11与GPIOB10
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;            //普通输出模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;       //100MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;           //推挽输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;             //上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);                   //初始化PA9，PA10
    GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); //GPIOB10复用为USART3
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); //GPIOB11复用为USART3
    //USART3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOB11与GPIOB10
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;             //复用功能
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        //速度50MHz
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;           //推挽复用输出
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;             //上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);                   //初始化PB10，PB11

    USART_InitStructure.USART_BaudRate = 115200;                //一般设置为 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为 8 位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;      //一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No;         //无奇偶校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
    USART_Init(USART_E34, &USART_InitStructure);                    //初始化串口
    USART_ClearFlag(USART_E34, USART_FLAG_TC | USART_FLAG_RXNE | USART_FLAG_IDLE);
    USART_ITConfig(USART_E34, USART_IT_RXNE, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;         //串口1中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;        //子优先级3
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           //IRQ通道使能  aPcCmuTxBuf_1
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(USART_E34, ENABLE); //使能串口
}

/*******************************************************************************
  * @brief  串口3中断接收              
  * @param  None              
  * @retval None              
  * @Note   None              
*******************************************************************************/
void USART3_IRQHandler(void) //串口1中断服务程序
{
    if ((USART_GetITStatus(USART3, USART_IT_RXNE) != RESET))
    {
        u8 data = USART_ReceiveData(USART3);
        if (ring_buffer_write(pE34_RX_BUF, data))
            led_set_value(0, 1);
    }
    //  USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}

/*********************************END OF FILE**********************************/
